const db = require('../db')
const brcypt = require('bcryptjs')


module.exports = (req, res) => {
    console.log(11111111111111111111)
    const usernameSql = 'SELECT * FROM user WHERE username=?'
    db(usernameSql, req.body.username, result => {
        if (result.length >= 1) {
            return res.send({
                status: 1,
                msg: '用户名已存在'
            })
        }
        const sql = 'INSERT INTO user set ?'
        req.body.password = brcypt.hashSync(req.body.password, 2)
        const { username, email, password } = req.body
        console.log("req.body", req.body);
        db(sql, { username, email, password }, result => {
            if (result.affectedRows === 1) {
                res.send({
                    status: 0,
                    msg: "注册成功"
                })
            } else {
                res.send({
                    status: 1,
                    msg: "注册失败"
                })
            }
        })
    })
}
